package com.longge;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-15 02:02
 */
public class SwapNodes {

    public ListNode swapNodes(ListNode head, int k) {

        ListNode pre = head, last = head, fast = head;
        int a = k, b = k;
        while (a > 1) {
            a--;
            pre = pre.next;
        }
        while (fast != null) {
            if (b > 0) {
                b--;
                fast = fast.next;
            }else {
                fast = fast.next;
                last = last.next;
            }

        }
        int temp = last.val;
        last.val = pre.val;
        pre.val = temp;
        return head;

    }
}
